package com.exe.project.logic;

import java.io.*;
import java.util.List;
import java.util.Iterator;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import com.exe.project.data.ReadXml;
import com.exe.project.interfaces.IReadXml;

/**
 * Servlet implementation class controllerFileUpload
 */
@WebServlet("/controllerFileUpload")
public class controllerFileUpload extends HttpServlet {
	private static final long serialVersionUID = 1L;
    private IReadXml readXml= new ReadXml();
    private RequestDispatcher view=null;
    private List listUploadFiles = null;
    private FileItem item = null;
    private FileItemFactory factory = new DiskFileItemFactory();
    private ServletFileUpload upload = new ServletFileUpload( factory );
    private PrintWriter out=null;
    private File archivo=null;
    private Iterator it=null;
    private String nombre="";
    /**
     * @see HttpServlet#HttpServlet()
     */
    public controllerFileUpload() {
        super();
    }

	private String dirUploadFiles;
	protected void processRequest(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
		response.setContentType("text/html;charset=UTF-8");
		out = response.getWriter();
		dirUploadFiles = getServletContext().getRealPath( getServletContext().getInitParameter("dirUploadFiles"));
		if( ServletFileUpload.isMultipartContent(request)){
			 try{
				 listUploadFiles = upload.parseRequest(request);
				 it = listUploadFiles.iterator();
			     while(it.hasNext()){
			         item = (FileItem) it.next();
			         if( !item.isFormField() ){
			             if( item.getSize() > 0 ){
			                 nombre = item.getName();
			                 if(nombre.substring(nombre.length()-3, nombre.length()).compareToIgnoreCase("xml")==0){
				                 archivo = new File(nombre);
				                 item.write(archivo);
				                 if (archivo.exists()){
				                	 readXml.setFiletoConv(archivo);
				                	 readXml.setDirectory((dirUploadFiles+"\\"));
				                	 readXml.convertXML();
					                 request.setAttribute("LinkArch", readXml.getPathArchive());
					                 view = request.getRequestDispatcher("resp.jsp");
				                 }else{
				                	 view = request.getRequestDispatcher("notConvert.jsp");
				                 }
			                 }else{
			                	 view = request.getRequestDispatcher("notConvert.jsp");
			                 }
			             }
			         }
			         
			     }
			 }catch( FileUploadException e ){
				 e.printStackTrace(); 
			 }catch (Exception e){
				 e.printStackTrace();
			 } 
			 view.forward(request, response);
		}
	}
	
	protected void doPost(HttpServletRequest request, HttpServletResponse response)	throws ServletException, IOException {
		processRequest(request, response);
	}

}
